<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Config extends CActiveRecord {
    public $query;
    public $startDate;
    public $endDate;
    public function rules() {
        return array(
            array('config_name, config_value,config_type,config_description', 'required'),
            array('config_name, config_description', 'length', 'max' => 250),
            array('config_name', 'unique'),
            array('config_value', 'length', 'max' => 65534)
        );
    }

    public function attributeLabels() {
        return array(
            'config_id' => Yii::t('default', '编号'),
            'config_name' => Yii::t('default', '配置名'),
            'config_value' => Yii::t('default', '配置值'),
            'config_type' => Yii::t('default', '类型'),
            'config_description' => Yii::t('default', '配置描述'),
            'config_add_title' => Yii::t('default', '添加配置'),
            'config_edit_title' => Yii::t('default', '修改配置'),
            'config_save_title' => Yii::t('default', '保存配置'),
            'config_create_time' => Yii::t('default', '创建时间'),
            'config_create_user' => Yii::t('default', '创建人'),
            'query' => Yii::t('default', '关键词'),
            'filterDate' => Yii::t('default', '时间过滤'),
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{config}}';
    }

    public function getConfig($name) {
        if ($name) {
            $where = new CDbCriteria();
            $where->select = 'config_value';
            $where->compare('config_name', $name);
            $result = $this->find($where);

            unset($where);
            if ($result) {
                return $result->config_value;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public function getSearch($key = null) {
        $criteria = new CDbCriteria();
        
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 25
                    ),
                    'sort' => array(
                        'defaultOrder' => 'config_id DESC', //设置默认排序是create_time倒序
                    ),
                ));
    }

}